package com.gdth.xml;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.Context;

import com.gdth.model.ImageItem;

/**
 * xml解析
 * @author Arvin
 *
 */
public class XmlPrase {
	/**
	 * 根据不同的tab类型获取菜单内容
	 * @param context
	 * @param type
	 * @return
	 */
	public List<ImageItem> getMenuList(Context context, String path){
		List<ImageItem> mList = new ArrayList<ImageItem>();
		InputStream ins = null;
		try {
			ins = context.getResources().getAssets().open(path);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		DocumentBuilderFactory factory = null;
        DocumentBuilder builder=null;
        Document document=null;
        
        try {
            factory = DocumentBuilderFactory.newInstance();;
            builder=factory.newDocumentBuilder();
            
            document=builder.parse(ins);
            
            //获取全部image 元素节点
            NodeList nodeAttributes=(NodeList) document.getElementsByTagName("type");
            
            int nodeLength = nodeAttributes.getLength();
            
            for(int i=0 ; i < nodeLength; i++){
            	Element mElement = (Element) (nodeAttributes.item(i));
            	
            	String id = mElement.getAttribute("key");
            	String name = mElement.getAttribute("name");
            	String imagename = mElement.getAttribute("imagename");
            	
            	ImageItem model = new ImageItem();
            	model.setKey(id);
            	model.setImg(imagename);
            	model.setValue(name);
            	
            	mList.add(model);
            }
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(); 
        } catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return mList;
	}
	
}
